package com.wyf.interceptor;

import com.wyf.pojo.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 非法访问请求拦截器
 */
public class LoginAccessInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 判断用户是否登录
        // 通过request对象获取session
        HttpSession session = request.getSession();
        // 获取session作用域中的用户信息
        User user = (User) session.getAttribute("user");
        // 判断user对象是否为空
        if (user == null)  {
            // 未登录状态，跳转登录页面
            response.sendRedirect(request.getContextPath() + "/login.jsp");
            return false;
        }

        // 如果用户是登录状态，则允许执行

        return true;
    }
}
